#include "ClientAPIBind.h"

string ClientAPIBind::to_string(ClientType type)
{
    if (type == Actor)
    {
        return "Actor";
    }
    else if (type == Viewer)
    {
        return "Viewer";
    }
    else
    {
        return "Error";
    }
}

bool ClientAPIBind::Connect(string host, int port)
{
    client = new httplib::Client(host + ':' + std::to_string(port));
    if (client == nullptr)
    {
        return false;
    }
    auto res = client->Get("/Hello");
    if (res)
    {
        if (res->status == 200)
        {
            return true;
        }
    }
    // auto err = res.error();
    // std::cout << "HTTP error: " << httplib::to_string(err) << std::endl;
    return false;
}

bool ClientAPIBind::Login(string token, ClientType type)
{
    httplib::Params params = {
        {"token", token},
        {"type", "Actor"}};
    httplib::Headers headers = {
        {"content-type", "application/json"}};
    auto res = client->Post("/Login", headers, params);
    if (res)
    {
        if (res->status == 200)
        {
            cout << res->body << endl;
            return true;
        } 
    }
    return false;
}

bool ClientAPIBind::Logout()
{
    return false;
}

ClientAPIBind::~ClientAPIBind()
{
    if (client != nullptr)
        delete client;
}
